package com.beiye.drivertransport.fragment;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.beiye.drivertransport.R;
import com.beiye.drivertransport.adapter.LearnPagerAdapter;

import java.lang.reflect.Field;
import java.util.ArrayList;




/**
 * Created by Lc on 2017/11/23.
 */
//安全学习（这种界面暂时没有用到）
public class SafeLearnFgt extends TwoBaseFgt {

//    @Bind(R.id.btn_first)
//    TextView leanrnbtn;
//    @Bind(R.id.btn_second)
//    TextView leanrnbtn2;
//    @Bind(R.id.tab_line)
//    View tab_line;

    private int TAB_MARGIN_DIP =35;

    ViewPager myviewpager;

    TabLayout TabLayout;
    //所有标题按钮的数组
 //   private TextView[] btnArgs;
    private ArrayList<Fragment> fragments;
    private LearnPagerAdapter adapter;
  //  ArrayList<LoginUserBean.DataBean.CurUserOrgBean> useList = new ArrayList<>();
    private ArrayList<String> list_title;

    @Override
    public int getLayoutId() {
        return R.layout.main_safe_learn_fgt;

    }

    @Override
    public void onClick(View view) {

    }
    //  @OnClick({R.id.btn_first,R.id.btn_second})
//    @Override
//    public void onClick(View view) {
//        resetButtonColor();
//        switch (view.getId()) {
//            case R.id.btn_first:
//
//                myviewpager.setCurrentItem(0);
//                adapter.notifyDataSetChanged();
//                leanrnbtn.setTextColor(Color.parseColor("#007eff"));
//                break;
//            case R.id.btn_second:
//                myviewpager.setCurrentItem(1);
//                adapter.notifyDataSetChanged();
//                leanrnbtn2.setTextColor(Color.parseColor("#007eff"));
//                break;
//        }
//
//    }

    @Override
    public void initData() {
        myviewpager = (ViewPager) getActivity().findViewById(R.id.myviewpager);
        TabLayout = (TabLayout) getActivity().findViewById(R.id.tab_FindFragment_title);

//      //  useData();
//        RelativeLayout le_botton =(RelativeLayout) getView().findViewById(R.id.bottonle);
//        le_botton.setOnClickListener(null);//解决导航顶部上面点击事件跳转首页广告详情页界面bug，应该不能跳转

//        //初始化按钮数组
//        btnArgs = new TextView[]{leanrnbtn,leanrnbtn2};

//        //先重置所有按钮颜色
//        resetButtonColor();
        //再将第一个按钮字体设置为红色，表示默认选中第一个
//        leanrnbtn.setTextColor(getActivity().getResources().getColor(R.color.project_blue));
//        leanrnbtn.setBackgroundResource(R.drawable.learnpressbtn);
        fragments = new ArrayList<Fragment>();
        fragments.add(new SpecifiedLearningFragment());
        fragments.add(new CourseSupermarketFragment());
        //将名称加载tab名字列表，正常情况下，我们应该在values/arrays.xml中进行定义然后调用
        list_title = new ArrayList<>();
        list_title.add("指定学习");
        list_title.add("课程超市");
        TabLayout.setTabMode(TabLayout.MODE_FIXED);
        TabLayout.addTab(TabLayout.newTab().setText(list_title.get(0)));
        TabLayout.addTab(TabLayout.newTab().setText(list_title.get(1)));


        adapter = new LearnPagerAdapter(getChildFragmentManager(),fragments,list_title);
        myviewpager.setAdapter(adapter);
        TabLayout.setupWithViewPager(myviewpager);
        setIndicator(getActivity(), TabLayout, TAB_MARGIN_DIP, TAB_MARGIN_DIP);
//        myviewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                //因为这里只有两个Tab,所有横条的宽带应该是屏幕的1/2
//                int lineWidth = getLineWidth(2);
//
//                //横条随着ViewPager一起滑动
//                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tab_line.getLayoutParams();
//                params.leftMargin = (int) ((positionOffset+ position )* lineWidth);
//                tab_line.setLayoutParams(params);
//            }
//
//            @Override
//            public void onPageSelected(int position) {
////                //每次滑动首先重置所有按钮的颜色
////                resetButtonColor();
////                //将滑动到的当前按钮颜色设置为红色
////                btnArgs[position].setTextColor(Color.WHITE);
////                btnArgs[position].setBackgroundResource(R.drawable.learnpressbtn);
//
//                //当ViewPager滑动完毕，改变标题颜色
//                changeTabColor(position);
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });




    }
    public void setIndicator(Context context, TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout ll_tab = null;
        try {
            ll_tab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) (getDisplayMetrics(context).density * leftDip);
        int right = (int) (getDisplayMetrics(context).density * rightDip);

        for (int i = 0; i < ll_tab.getChildCount(); i++) {
            View child = ll_tab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
    public  DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric;
    }

    public  float getPXfromDP(float value, Context context) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
                context.getResources().getDisplayMetrics());
    }

//    /**
//     *  根据标题的个数获取横条应该设置的宽度
//     * @param tabCount
//     * @return
//     */
//    public int getLineWidth(int tabCount){
//        DisplayMetrics metric = new DisplayMetrics();
//        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
//        int lineWidth = metric.widthPixels/tabCount;
//        return lineWidth;
//    }
//    /**
//     *
//     * 根据ViewPager的位置改变对应标题的
//     * @param position
//     */
//    private void changeTabColor(int position){
//        resetButtonColor();
//
//        if(0 == position) {
//            leanrnbtn.setTextColor(Color.parseColor("#007eff"));
//        }
//
//        if(1 == position) {
//            leanrnbtn2.setTextColor(Color.parseColor("#007eff"));
//        }


  //  }
//    //重置所有按钮的颜色
//    public void resetButtonColor(){
//        leanrnbtn.setTextColor(Color.parseColor("#7f7f83"));
//        leanrnbtn2.setTextColor(Color.parseColor("#7f7f83"));
//
//    }

//    //获取当前用户机构信息
//    private void useData()
//    {
//        LoginUserBean userInfo = UserManger.getUserInfo();
//        String userId = userInfo.getData().getUserId();
//        OkGo.get(AppInterfaceConfig.BASE_URL+"user/userAccount/userOrg/"+userId)
//                .tag(this)
//                .execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(String s, Call call, Response response) {
//                        UserBean userBean = JSON.parseObject(s, UserBean.class);
//                        List<LoginUserBean.DataBean.CurUserOrgBean> rows = userBean.getRows();
//                        useList.clear();
//                        useList.addAll(rows);
//                        LoginUserBean userInfo = UserManger.getUserInfo();
//                        userInfo.getData().setCur_userOrg(useList);
//                        UserManger.setUserInfo(userInfo);
//
//                    }
//
//                    @Override
//                    public void onError(Call call, Response response, Exception e) {
//                        super.onError(call, response, e);
//
//                    }
//                });


 //   }

    @Override
    public void requestData() {



    }

    @Override
    public void onUserVisible() {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
}
